<template>
  <div class="proDialog">
    <el-dialog title="选择项目"
      :visible.sync="visible"
      width="1000px"
      z-index="1500"
      append-to-body
      @close="toCancelBox">
      <el-form class="formList"
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="80px"
        style="padding-top: 0">

        <el-form-item label="项目名称"
          prop="itemName">
          <el-input v-model="queryParams.itemName"
            placeholder="请输入项目名称"
            clearable
            size="small" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table border
        ref="indexTable"
        :data="infoList"
        tooltip-effect="dark"
        style="width: 100%"
        @row-click="singleElection">

        <el-table-column label=""
          width="40">
          <template slot-scope="scope">
            <el-radio class="radio"
              v-model="templateSelection"
              :label="scope.$index"></el-radio>
          </template>
        </el-table-column>

        <el-table-column prop="deviceImage"
          label="图片"
          align="center"
          width="120">
          <template slot-scope="scope">
            <el-image v-if="
              scope.row.itemMainImage != null && scope.row.itemMainImage != ''
            "
              slot="reference"
              :src="scope.row.itemMainImage"
              class="image-rows"
              :preview-src-list="[scope.row.itemMainImage]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="itemName"
          label="项目名称"
          width="100"
          align="left" />
        <el-table-column label="面价"
          align="center"
          prop="itemPriceStr" />
        <el-table-column label="活动价格"
          align="center"
          prop="itemFavorablePriceStr" />
        <el-table-column label="结算价格"
          align="center"
          prop="itemSettlementPriceStr" />
        <el-table-column label="项目类别"
          align="center"
          prop="itemTypeStr" />
        <el-table-column label="是否投放门店"
          align="center"
          prop="isCommon">
          <template slot-scope="scope">
            <span>{{ scope.row.isCommon == '1' ? '通用' : '指定门店' }}</span>
          </template>
        </el-table-column>

      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="pageSizess" />

      <div style="display:flex; justify-content: flex-end;">
        <el-button @click="determine"
          type="primary">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import {
  listProboxServiceItem
} from "@/api/service/item"
export default {
  props: {
    flag: {
      type: Boolean,
    },
    itemUid: {
      type: Number,
    }
  },
  watch: {
    flag (val) {
      this.visible = this.flag;
    },
  },
  data () {
    return {
      templateSelection: '',
      // 显示搜索条件
      showSearch: true,
      datas: null,
      selectedDatas: [],
      pageSizess: [5, 10, 15, 20, 25, 30, 50],
      // 商品信息表格数据
      infoList: [],
      // indexTable: [],
      infoListHistory: [],
      multipleSelection: [],
      multipleSelectionData: {},
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        itemName: null,
        noItemId: this.itemUid,
      },
      visible: false,
      categoryOptions: []
    };
  },
  created () {
    this.visible = this.flag;
    this.getList();

  },
  methods: {
    singleElection (row) {
      this.multipleSelectionData = row;
      //let data = JSON.parse(JSON.stringify(row))
      this.templateSelection = this.infoList.indexOf(row);
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    toCancelBox () {
      this.visible = false;
      this.$emit("toCloseBox", false);
    },
    //回显选中
    greet (selectedData) {
      if (Object.keys(selectedData).length > 0) {
        this.multipleSelectionData = selectedData;
        // this.templateSelection = selectedData.itemId;
      } else {
        this.multipleSelectionData = {};
        // this.templateSelection = '';
      }
      this.singleElection(this.multipleSelectionData)

      // this.selectedDatas.forEach((item) => {
      //   this.$refs.indexTable.toggleRowSelection(item, true);
      // });
    },
    bindRowKeys (row) {
      return row.itemId;
    },
    handleSelectionChange (val) {
      this.datas = this.multipleSelection;
      this.multipleSelection = val
      if (this.datas.length > 0) {
        for (let i = 0; i < this.datas.length; i++) {
          for (let k = 0; k < this.multipleSelection.length; k++) {
            if (this.datas[i].itemId == this.multipleSelection[k].itemId) {
              this.multipleSelection[k] = this.datas[i]
            }
          }
        }
      }
    },
    /** 确定选择商品 */
    determine () {
      this.$emit("multipleSelection", this.multipleSelectionData);

      this.$forceUpdate();
      this.open = false;
    },
    /** 查询商品信息列表 */
    getList () {
      listProboxServiceItem(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (Object.keys(this.multipleSelectionData).length > 0) {
          setTimeout(() => {
            let index = -1;
            this.infoList.forEach((vo, ind) => {
              if (this.multipleSelectionData.itemId == vo.itemId) {
                index = ind
              }
            })
            this.templateSelection = index;
          });

        }
      });
    },
  },
};
</script>
<style scoped>
.determine {
  position: relative;
  color: #fff;
}

.pagination-container {
  position: relative;
}

.image-rows {
  width: 80px;
  height: 80px;
}

/deep/ .el-radio__label {
  display: none;
}
</style>
